<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/28
 * Time: 9:57
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class ChannelController extends CommonController{
    public function index(){
        //查渠道数据赋给模板
        $count = M("channel")->count();// 查询满足要求的总记录数
        $Page  = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $show = $Page->show();// 分页显示输出
        $channel=M("channel")->order("company")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign("page",$show);//把分页数据赋给模板
        $this->assign("channel",$channel);
        $this->display();
    }
    //渠道的添加
    public function add(){
        if(!IS_POST) {
            $this->display();
        }else{
            $data=I("post.");
            $test=M("channel")->data($data)->add();
            if($test) {
                $this->success('操作成功！',__MODULE__.'/Channel/index');
            } else {
                $this->error('操作失败！',__MODULE__.'/Channel/index');
            }
        }

    }
    //渠道下添加分区
    public function limits() {
        $channel_id = intval($_REQUEST['channel_id']);
        if(IS_POST) {
            $mod = M("channel_partition");
            $partition_id=$_POST['partition_id'];
            $cid=$mod->field("partition_id")->where("channel_id={$channel_id}")->select();
            $counid=[];
            foreach($cid as $va){
                $counid[]=$va['partition_id'];
            }
            $dc=array_diff($counid,$partition_id);
            $ac=array_diff($partition_id,$counid);
            if(!empty($dc)){
                foreach($dc as $v){
                    $bool =$mod->where("partition_id={$v} and channel_id={$channel_id}")->delete();
                }
            }elseif(!empty($ac)){
                foreach($ac as $val){
                    $bol =$mod->data(array("partition_id"=>$val,"channel_id"=>$channel_id))->add();
                }
            }else{
                $this->error("没有勾选分区");
            }
            if($bol||$bool){
                $this->success("操作成功",__MODULE__.'/channel/index')  ;
            }else{
                $this->error("操作失败");
            }
        } else {
            $partition = M("partition")->field("id,partition")->order("partition")->select();
            $cp = M("channel_partition")->field("partition_id")->where("channel_id={$channel_id}")->select();
            foreach($cp as $value){
                $cpid[]=$value['partition_id'];
            }
            $this->assign("pc",$cpid);
            $this->assign("partition",$partition);
            $this->assign("channel_id",$channel_id);
            $this->display();
        }
    }
    //渠道的修改
    public function edit(){
        if(IS_POST) {
            $id= I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/Channel/index');
            }else{
                $data=I("post.");
                $test=M("channel")->data($data)->where("id={$id}")->save();
                if($test) {
                    $this->success('操作成功！',__MODULE__.'/Channel/index');
                } else {
                    $this->display("Public:error");
                }

            }
        }else{
            $id=I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/Channel/index');
            }else{
                $arr=M("channel")->where("id={$id}")->select();

                $this->assign("arr",$arr);
                $this->display();

            }
        }
    }
    //渠道的删除
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/Channel/index');
        }
        if(M("channel")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/Channel/index');
        } else {
            $this->error('操作失败！',__MODULE__.'/Channel/index');
        }
    }
    //方法
    public function _empty(){
        $this->error("404");
    }
}